home *** CD-ROM | disk | FTP | other *** search
/ Gigarom 1 / Gigarom Macintosh Archives (Quantum Leap)(CDRM1080320)(1993).iso / FILES / HYP / C-D / DeveloperStax.cpt / Developer Stack 1.0 / card_8432.txt < prev    next >
Text File  |  1989-02-26  |  8KB  |  238 lines

  1. -- card: 8432 from stack: in.0
  2. -- bmap block id: 0
  3. -- flags: 0000
  4. -- background id: 14090
  5. -- name: Text Import
  6.  
  7.  
  8. -- part 1 (button)
  9. -- low flags: 00
  10. -- high flags: A004
  11. -- rect: left=191 top=266 right=297 bottom=349
  12. -- title width / last selected line: 0
  13. -- icon id / first selected line: 0 / 0
  14. -- text alignment: 1
  15. -- font id: 0
  16. -- text size: 12
  17. -- style flags: 0
  18. -- line height: 16
  19. -- part name: Import Text Button
  20. ----- HyperTalk script -----
  21. --File Importer Button 1.1
  22. --Copyright ¬© 1987 Stephen Michel 1027 Pomona, Albany, CA 94706
  23. --9-3-87
  24. --More notes at the end of the script
  25.  
  26. on mouseUp
  27.   if the optionkey is down then
  28.     edit script of target
  29.     exit mouseup
  30.   end if
  31.  
  32.   global inFile
  33.   global error
  34.   newStack
  35.   show message box at 33,296
  36.   getFile
  37.   if error is not empty then
  38.     answer "Import Stopped!" with "Okay"
  39.     exit mouseUp
  40.   end if
  41.   goodFile
  42.   if error is not empty then
  43.     exit mouseUp
  44.   end if
  45.   createStruct
  46.   importText
  47.   choose browse tool
  48. end mouseUp
  49.  
  50. on newStack
  51.   global inFile
  52.   global error
  53.   -- Creates the new stack
  54.   doMenu New Stack...
  55. end newStack
  56.  
  57. on getFile
  58.   global inFile
  59.   global error
  60.   -- finds the file to import from
  61.   put FileName("TEXT") into it
  62.   if it is empty then
  63.     put "true" into error
  64.   else
  65.     put empty into error
  66.     put it into inFile
  67.   end if
  68. end getFile
  69.  
  70. on createStruct
  71.   -- right now, this just creates a structure for a tab-delimited file
  72.   global inFile
  73.   global error
  74.   global countFields
  75.   put 0 into countFields
  76.   open file inFile
  77.   put "Now analyzing the structure & counting fields..."
  78.   put "dummy" into it
  79.   -- simply loops through the first record until it hits a return
  80.   -- and adds one to countFields every time it finds a tab
  81.   repeat until it is empty
  82.     read from file inFile until tab
  83.     add 1 to countFields
  84.     if it contains return then
  85.       exit repeat
  86.     end if
  87.   end repeat
  88.   close file inFile
  89.   put "There are " & countFields & " fields in the file"
  90.   domenu "background"
  91.   open file inFile
  92.   -- now we are creating the strucutre, naming the fields, placing them
  93.   -- show the data in the first record as the field name (in case the
  94.   -- field names in the first record & to help in remembering the data
  95.   -- to assign names to the fields)
  96.   repeat with x = 1 to countFields - 1
  97.     read from file inFile until tab
  98.     delete last character of it
  99.     ask "Please type a name for field" && x with it
  100.     put it into fieldName
  101.     newField fieldName
  102.   end repeat
  103.   -- this does the same for the last field
  104.   read from file inFile until return
  105.   ask "Please type a name for field" && countFields with it
  106.   put it into fieldName
  107.   newfield fieldName
  108.   -- all done!
  109.   domenu "background"
  110.   close file inFile
  111. end createStruct
  112.  
  113. on importText
  114.   global inFile
  115.   global error
  116.   global countFields
  117.   open file inFile
  118.   put "dummy" into it
  119.   put 1 into recNum
  120.   -- loops through the cards & fields until the end of file
  121.   -- this usually creates one or two extra cards, but it
  122.   -- was easy to code.
  123.   put the seconds into startTime
  124.   repeat until it is empty
  125.     put "Now on record #" & recNum
  126.     repeat with x = 1 to countFields -1
  127.       read from file inFile until tab
  128.       -- this line removes the tab
  129.       delete last character of it
  130.       repeat while it contains quote
  131.         delete char offset(quote,it) of it
  132.       end repeat
  133.       put it into field x
  134.     end repeat
  135.     -- now do the same for the last field
  136.     read from file inFile until return
  137.     put it into field countFields
  138.     domenu "new card"
  139.     add 1 to recNum
  140.   end repeat
  141.   -- all done!
  142.   put the seconds into stopTime
  143.   close file inFile
  144.   put stopTime - startTime into timeTook
  145.   divide timeTook by 60
  146.   hide message window
  147.   answer "Done!" && recNum && "records written" with "Ok"
  148. end importText
  149.  
  150. on goodFile
  151.   -- checks to make sure the file exists
  152.   -- Hypercard automatically creates a new file when you
  153.   -- issue the "open" command.  So we read from the file
  154.   -- to see if there is anything in it.
  155.   -- Be sure to delete any excess files created by this.
  156.   -- You can probably type in a path name to a file, if it is not
  157.   -- in the same folder as HyperCard
  158.   global inFile
  159.   global error
  160.   open file inFile
  161.   read from file inFile until return
  162.   if it is empty then
  163.     put "That file does not exist"
  164.     put "true" into error
  165.     close file inFile
  166.     exit goodFile
  167.   end if
  168.   close file inFile
  169. end goodFile
  170.  
  171. on newField fieldName
  172.   -- this routine simply creates the field
  173.   -- this lets us add to a background that already contains fields
  174.   put the number of background fields into numFields
  175.   put numFields + 1 into thisField
  176.   put "click where you want the field to go"
  177.   wait until the mouseClick
  178.   put the mouseLoc into here
  179.   domenu "new field"
  180.   put first item of here into right
  181.   -- fixed current length of field on screen
  182.   add 150 to right
  183.   put second item of here into bottom
  184.   add 20 to bottom
  185.   put here && ","&& right && "," && bottom into thePlace
  186.   -- these can be changed for different field defaults
  187.   set rect of background field thisField to thePlace
  188.   set style of background field thisField to rectangle
  189.   set name of background field thisField to fieldName
  190. end newField
  191.  
  192. -- This button is self-contained.  That means you can place the button
  193. -- in any background you like & use it to create a stack. It will first
  194. -- create a new stack (with the usual HyperCard "New Stack" dialogue).
  195.  
  196.  
  197.  
  198.  
  199.  
  200. -- part 9 (field)
  201. -- low flags: 00
  202. -- high flags: 0007
  203. -- rect: left=64 top=79 right=258 bottom=501
  204. -- title width / last selected line: 0
  205. -- icon id / first selected line: 0 / 0
  206. -- text alignment: 0
  207. -- font id: 3
  208. -- text size: 9
  209. -- style flags: 0
  210. -- line height: 12
  211. -- part name: 
  212.  
  213.  
  214. -- part contents for background part 5
  215. ----- text -----
  216. 3
  217.  
  218. -- part contents for background part 12
  219. ----- text -----
  220. Text Import
  221.  
  222. -- part contents for card part 9
  223. ----- text -----
  224. IMPORTER 1.1                                     
  225. Copyright ¬© 1987 Stephen Michel
  226. 1027 Pomona, Albany, CA 94706  CompuServe: 70611,1215
  227. If you like it, please send $10.  Thanx.
  228.  
  229. ABOUT THIS BUTTON: It will read any TAB DELIMITED file into a new stack, allow you to name fields to hold the data, and do some crude layout of the form.  It will provide a relatively complete HyperCard database that includes next, previous, and home buttons -- the rest is up to you.  
  230.  
  231. You will be asked first for the name of the new stack.  Next it will want to know what text file file to import - just select it.   The script will then read the file, figure out how many fields are in it and ask for the name of each field.  Each field name defaults to the data that is in that field in the first record of the file -- to help you remember what each field is, or if you have put the field names at the top, to make it easier.
  232.  
  233. LIMITATIONS:  There are many: 1) As of now, it will only read tab delimited fields, though I am working on other formats (such as mailmerge, comma delimited, SYLK, etc.). 2) All the created fields are the same size on screen.  This simplified the coding, and it shouldn't be too hard to go back and adjust them. 
  234.  
  235. CHANGES: Aside from making it more flexible in types of files it can read, it could go faster by locking the screen, so every record doesn't have to get written.  But I like to see what is going on.  You can also customize this by placing the "import" button onto a background you like better -- it is  self contained.
  236.  
  237. Clicking "cancel" in most dialogues will NOT stop the entire operation -- I haven't done all the error trapping yet.  Typing command-period will stop it, though it will leave the source file open.  Type "close file" followed by the file name into the message box to close the file.  I have worked with files of up to 500 records, and it does take some time (about 10 minutes to read 200 or so records into a stack).
  238.